[id].tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { Head } from "fresh/runtime";
  2. import { createDefine, HttpError } from "fresh";
  3. import type { PageProps } from "fresh";
  4. import { checkToken } from "utils/server.ts";
  5. import { find } from "utils/db.ts";
  6. import TopBar from "../islands/TopBar.tsx";
  7. import Editor, { EditorMode } from "../islands/Editor.tsx";
  8. const define = createDefine<Record<never, never>>();
  9. interface PostProps {
  10. id: string;
  11. title: string;
  12. content: string;
  13. shared: boolean;
  14. isLogined: boolean;
  15. allowMode: EditorMode;
  16. }
  17. export const handler = define.handlers<PostProps>({
  18. GET(ctx) {
  19. const tokenUserId = checkToken(ctx.req);
  20. const postId = ctx.params.id;
  21. const post = find(
  22. "Post",
  23. tokenUserId
  24. ? { id: postId, user_id: tokenUserId }
  25. : { id: postId, shared: true },
  26. ["title", "content", "shared"],
  27. );
  28. if (post.length > 0) {
  29. return {
  30. data: {
  31. id: postId,
  32. isLogined: Boolean(tokenUserId),
  33. allowMode: tokenUserId ? EditorMode.Both : EditorMode.Read,
  34. title: post[0][0] as string,
  35. content: post[0][1] as string,
  36. shared: post[0][2] as boolean,
  37. },
  38. };
  39. }
  40. throw new HttpError(404);
  41. },
  42. });
  43. export default function Post(props: PageProps<PostProps>) {
  44. return (
  45. <>
  46. <Head>
  47. <title>{props.data.title}</title>
  48. </Head>
  49. <div className="pd-page">
  50. <TopBar
  51. id={props.data.id}
  52. title={props.data.title}
  53. shared={props.data.shared}
  54. allowMode={props.data.allowMode}
  55. isLogined={props.data.isLogined}
  56. />
  57. <Editor
  58. id={props.data.id}
  59. content={props.data.content}
  60. allowMode={props.data.allowMode}
  61. />
  62. </div>
  63. </>
  64. );
  65. }